﻿/*****************************************************************************************************************
* This code is taken from http://mr-sharpoblunto.junkship.org/2008/07/silverlight-one-step-forward-one-step.html *
*TODO: We must re write this class                                                                               *
 *****************************************************************************************************************/

using System.Configuration;
using System.IO;
using System.Net;
using System.Web;

//This module implements IHttpHandler (ashx file redirects here) to solve communication using requests and
//processing reponses
namespace SoapProxyComponent
{
    public class SoapProxyHandler : IHttpHandler
    {
        private SoapProxyConfiguration _config;

        public SoapProxyHandler()
        {
            _config = (SoapProxyConfiguration)ConfigurationManager.GetSection("soapProxyComponent");
        }

        public void ProcessRequest(HttpContext context)
        {
            foreach (MappingElement endPointMapping in _config.EndPointMappings)
            {
                if (context.Request.Path.Contains(endPointMapping.ProxyEndPoint))
                {
                    ProxyCall(endPointMapping.RemoteEndPoint, context.Request, context.Response);
                    return;
                }
            }


            context.Response.ContentType = "text/xml";
            context.Response.Write("<error>No matching proxy endpoint found for '" + context.Request.Path + "'</error>");
        }

        private void ProxyCall(string endPoint, HttpRequest request, HttpResponse response)
        {
            HttpWebRequest proxyRequest = (HttpWebRequest)WebRequest.Create(endPoint);
            proxyRequest.Method = request.HttpMethod;
            proxyRequest.ContentType = request.ContentType;
            proxyRequest.ContentLength = request.ContentLength;

            Stream requestStream = request.InputStream;
            Stream proxyRequestStream = proxyRequest.GetRequestStream();

            byte[] buffer = new byte[255];
            int bytesRead = requestStream.Read(buffer, 0, 255);

            while (bytesRead > 0)
            {
                proxyRequestStream.Write(buffer, 0, bytesRead);
                bytesRead = requestStream.Read(buffer, 0, 255);
            }
            proxyRequestStream.Close();

            HttpWebResponse proxyResponse = (HttpWebResponse)proxyRequest.GetResponse();
            response.ContentType = proxyResponse.ContentType;

            Stream proxyResponseStream = proxyResponse.GetResponseStream();
            StreamReader responseStreamReader = new StreamReader(proxyResponseStream);
            response.Write(responseStreamReader.ReadToEnd());
            responseStreamReader.Close();
        }

        public bool IsReusable
        {
            get
            {
                return true;
            }
        }
    }
}
